package com.fyqiang.api;



import com.fyqiang.api.enums.ApiResponseEnum;
import com.fyqiang.common.enums.BaseEnum;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

public class APIResponse<T> implements Serializable {


    private static final long serialVersionUID = 5241526151768786394L;

    private final String ver = "1.1.0000";
    private boolean ret;
    private String errmsg = "";
    private int errcode;
    private T data;
    private String sign = "";
    private Long serverTime;//服务器时间

    public APIResponse() {
        this.setApiResponseEnum(ApiResponseEnum.SUCCESS);
    }

    private APIResponse(T t) {
        this();
        this.data = t;
    }

    private APIResponse(ApiResponseEnum ret) {
        this.setApiResponseEnum(ret);
    }

    private APIResponse(BaseEnum ret, T t) {
        this.setApiResponseEnum(ret);
        this.data = t;
    }

    private APIResponse(String errmsg, T t) {
        this.errmsg = errmsg;
        this.data = t;
    }

    public static <T> APIResponse<T> returnSuccess() {
        return new APIResponse();
    }

    public static <T> APIResponse<T> returnSuccess(T data) {
        return new APIResponse(ApiResponseEnum.SUCCESS, data);
    }

    public static <T> APIResponse<T> returnSuccess(T data, BaseEnum ret) {
        return new APIResponse(ret, data);
    }

    public static <T> APIResponse<T> returnSuccess(T data, String errmsg) {
        return new APIResponse(errmsg, data);
    }

    public static <T> APIResponse<T> returnSuccess(BaseEnum ret) {
        return new APIResponse(ret);
    }

    public static <T> APIResponse<T> returnFail(BaseEnum ret, String appendErrmsg, T data) {
        APIResponse apiResponse = returnFail(ret, appendErrmsg);
        apiResponse.setData(data);
        return apiResponse;
    }

    public static <T> APIResponse<T> returnFail(BaseEnum ret, String appendErrmsg) {
        APIResponse apiResponse = returnFail(ret);
        if (appendErrmsg != null) {
            apiResponse.errmsg = apiResponse.errmsg + "（" + appendErrmsg + "）";
        }
        return apiResponse;
    }

    public static <T> APIResponse<T> returnFail(BaseEnum ret) {
        APIResponse apiResponse = new APIResponse();
        apiResponse.ret = false;
        apiResponse.errmsg = ret.getLabel();
        apiResponse.errcode = ret.getId();
        return apiResponse;

        //return new APIResponse(ret); // @2016/06/01 zhuo.shi 删除此行，此行会返回SUCCESS，不是Fail信息
    }


    public static <T> APIResponse<T> returnFail(String msg) {
        APIResponse apiResponse = new APIResponse();
        apiResponse.ret = false;
        apiResponse.errmsg = msg;
        apiResponse.errcode = ApiResponseEnum.FAIL.getId();
        return apiResponse;
    }

    public static <T> APIResponse<T> returnFail(T data, BaseEnum ret) {
        return returnSuccess(data, ret);
    }

    public static <T> APIResponse<T> returnFail(T data, String errmsg) {
        return returnSuccess(data, errmsg);
    }

    public static <T> APIResponse<T> returnFail(int errorCode, String errorMessage) {
        APIResponse apiResponse = new APIResponse();
        apiResponse.errcode = errorCode;
        apiResponse.errmsg = errorMessage;
        apiResponse.ret = false;
        return apiResponse;
    }

    public static <T> APIResponse<T> returnSuccess(int errorCode, String errorMessage, T data) {
        APIResponse apiResponse = new APIResponse();
        apiResponse.errcode = errorCode;
        apiResponse.errmsg = errorMessage;
        apiResponse.ret = true;
        apiResponse.setData(data);
        return apiResponse;
    }

    public Long getServerTime() {
        return serverTime;
    }

    public void setServerTime(Long serverTime) {
        this.serverTime = new Date().getTime();
    }

    public String getVer() {
        return ver;
    }

    public boolean isRet() {
        return ret;
    }

    public String getErrmsg() {
        return StringUtils.defaultIfEmpty(errmsg, StringUtils.EMPTY);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getErrcode() {
        return errcode;
    }


    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setApiResponseEnum(BaseEnum apiResponseEnum) {
        this.ret = apiResponseEnum.isSuccess();
        this.errcode = apiResponseEnum.getId();
        this.errmsg = apiResponseEnum.getLabel();
        this.serverTime = new Date().getTime();
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((data == null) ? 0 : data.hashCode());
        result = prime * result + ((StringUtils.isEmpty(errmsg)) ? 0 : errmsg.hashCode());
        result = prime * result + (ret ? 1231 : 1237);
        result = prime * result + errcode;
        result = prime * result + ((ver == null) ? 0 : ver.hashCode());
        return result;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        APIResponse<?> that = (APIResponse<?>) o;

        if (errcode != that.errcode) {
            return false;
        }
        if (ret != that.ret) {
            return false;
        }
        if (data != null ? !data.equals(that.data) : that.data != null) {
            return false;
        }
        if (errmsg != null ? !errmsg.equals(that.errmsg) : that.errmsg != null) {
            return false;
        }
        return !(ver != null ? !ver.equals(that.ver) : that.ver != null);

    }

//    @Override
//    public String toString() {
//        return JSON.toJSONString(this);
//    }


}
